********************************************************************************
* FAC DATA DIAGNOSTIC
* Identify exactly which quarters are missing and why
********************************************************************************

clear all
set more off

cd "/Users/ciaran/PycharmProjects/fac"

********************************************************************************
* STEP 1: What quarters SHOULD exist (1968Q1 - 2025Q3)
********************************************************************************

clear
set obs 230  // 57 years * 4 quarters + 2 quarters (2025 Q1-Q3 minus Q4)

gen n = _n
gen year = 1968 + floor((n-1)/4)
gen quarter = mod(n-1, 4) + 1

* Adjust for 2025 (only Q1-Q3)
drop if year == 2025 & quarter == 4
drop if year > 2025

gen yq_expected = yq(year, quarter)
format yq_expected %tq

di "Expected quarters: " _N
save "temp_expected_quarters.dta", replace

********************************************************************************
* STEP 2: What quarters do we HAVE in fac_fred_merged
********************************************************************************

use "fac_fred_merged.dta", clear

* Keep only identifiers
keep year quarter
gen has_merged = 1

di "Quarters in merged file: " _N
save "temp_merged_quarters.dta", replace

********************************************************************************
* STEP 3: Find the gaps
********************************************************************************

use "temp_expected_quarters.dta", clear
merge 1:1 year quarter using "temp_merged_quarters.dta"

* List missing quarters
di ""
di "=============================================="
di "MISSING QUARTERS (in expected but not in merged)"
di "=============================================="
list year quarter if _merge == 1, noobs

* Count by decade
di ""
di "Missing by decade:"
gen decade = floor(year/10)*10
tab decade if _merge == 1

* Count by year for recent period
di ""
di "Missing quarters 2000-2025:"
list year quarter if _merge == 1 & year >= 2000, noobs

********************************************************************************
* STEP 4: Clean up
********************************************************************************

erase "temp_expected_quarters.dta"
erase "temp_merged_quarters.dta"

di ""
di "=============================================="
di "SUMMARY"
di "=============================================="
di "Expected quarters (1968Q1-2025Q3): 230"
count if _merge == 1
di "Missing quarters: " r(N)
count if _merge == 3
di "Present quarters: " r(N)
di "=============================================="

********************************************************************************
